package com.aptemo.webetek.client.widgets.teams;

import java.util.ArrayList;
import java.util.List;

import com.aptemo.webetek.client.WebetekUserServiceAsync;
import com.aptemo.webetek.client.commands.impl.GetTeamsAction;
import com.aptemo.webetek.client.commands.responses.GotTeamList;
import com.aptemo.webetek.client.events.AddTeamEvent;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.client.mvp.BaseDisplay;
import com.aptemo.webetek.client.widgets.paging.PagingController;
import com.aptemo.webetek.client.widgets.paging.PagingDisplay;
import com.aptemo.webetek.shared.TeamBean;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasKeyPressHandlers;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;

public class UserGroupsPresenter
		extends PagingController<TeamBean>
{

	public interface Display extends BaseDisplay
	{
		HasClickHandlers createTeamBtn();

		HasValueChangeHandlers<Boolean> getShowMineOnlyGroups();

		HasValue<Boolean> getShowMineOnlyGroupsValue();

		HasClickHandlers getList();

		HasValueChangeHandlers<String> getFilterTextBox();

		HasKeyPressHandlers getFilterTextBoxKeyPressed();

		HasChangeHandlers getFilterTextBoxChange();

		HasValue<String> getFilterTextBoxValue();

		void updateTeams(List<TeamBean> list, HandlerManager evenbus, WebetekUserServiceAsync rpc);

		void addAdminMenu();

		int getClickedRow(ClickEvent event);

		HasText getInfoField();

		PagingDisplay getPagingView();

		void setPagingMode(boolean showPaging);

		String translate(String string);

	}

	private HandlerManager			meventBus;
	private WebetekUserServiceAsync	mservice;
	private Display					display;
	// private UserBean muserInfo;
	private ArrayList<TeamBean>		list;
	private boolean					isAdmin;
	private int						pageNo	= 0;

	public UserGroupsPresenter(boolean isAdmin, Display view, HandlerManager eventbus, WebetekUserServiceAsync rpc)
	{
		this(isAdmin, view, eventbus, rpc, false, 0);
	}

	public UserGroupsPresenter(boolean isAdmin, Display view, HandlerManager eventBus, WebetekUserServiceAsync rpcService, boolean showPaging, int pageNo)
	{
		super(view.getPagingView(), 5);
		meventBus = eventBus;
		mservice = rpcService;
		display = view;
		this.pageNo = pageNo;
		// this.showPaging = showPaging;
		this.isAdmin = isAdmin;
	}

	private void bind()
	{
		// display.setPagingMode(showPaging);

//		meventBus.addHandler(TeamUpdatedEvent.TYPE, new TeamUpdatedHandler()
//		{
//
//			@Override
//			public void onTeamUpdated(String id)
//			{
//				retrieveTeams();
//			}
//		});

		// //////////////////////////////////
		// Display handlers---
		display.createTeamBtn().addClickHandler(new ClickHandler()
		{
			@Override
			public void onClick(ClickEvent event)
			{
				meventBus.fireEvent(new AddTeamEvent());
			}
		});

		display.getShowMineOnlyGroups().addValueChangeHandler(new ValueChangeHandler<Boolean>()
		{

			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event)
			{
				// reload list from backend
				retrieveTeams();
			}

		});

		display.getFilterTextBox().addValueChangeHandler(new ValueChangeHandler<String>()
		{

			@Override
			public void onValueChange(ValueChangeEvent<String> event)
			{
				String filter = event.getValue();
				renderList(list, filter);
			}
		});

		display.getFilterTextBoxChange().addChangeHandler(new ChangeHandler()
		{

			@Override
			public void onChange(ChangeEvent event)
			{
				String filter = display.getFilterTextBoxValue().getValue();
				renderList(list, filter);
			}
		});

		display.getList().addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				// event.stopPropagation();
				// int i = display.getClickedRow(event);
				// UserGroupBean team = list.get(i);
				//
				// meventBus.fireEvent(new UserGroupChangedEvent(team.getId(),
				// ""));

			}
		});

	}

	private void retrieveTeams()
	{
		display.getInfoField().setText(display.translate("loading"));

		// service
		GetTeamsAction action = new GetTeamsAction(!display.getShowMineOnlyGroupsValue().getValue());
		mservice.invoke(action, new GotTeamList()
		{

			@Override
			public void got(ArrayList<TeamBean> result)
			{
				list = result;

				//
				display.getInfoField().setText("");

				renderList(result, "");
			}

			@Override
			public void onError(String message, EErrorCodes code)
			{
				display.getInfoField().setText(message);
			}

		});
	}

	private void renderList(List<TeamBean> list, String filter)
	{

		if (isAdmin)
		{
			display.addAdminMenu();
		}

		filter = filter.toLowerCase();

		List<TeamBean> temp = new ArrayList<TeamBean>();
		// filtering...
		for (TeamBean item : list)
		{

			if (filter.isEmpty() || item.getName().toLowerCase().startsWith(filter))
			{
				temp.add(item);
			}
		}

		//

		setList(temp, pageNo);

	}

	@Override
	public void go(HasWidgets container)
	{
		bind();
		container.clear();
		container.add(display.getDisplay());

		retrieveTeams();
	}

	@Override
	public void refreshPaging(List<TeamBean> data)
	{
		display.updateTeams(data, meventBus, mservice);
	}
}
